import datetime

import jsonpickle
import pytz
from django.conf import settings
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.hashers import check_password, make_password
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
from search.function import get_msg, send_email

from .models import AccountUser


def index(request):
    user = request.session.get('login', None)
    veremail = request.GET.get('veremail', None)
    changemail = request.GET.get('changemail', None)
    msg, errormsg = get_msg(request)
    if user:
        user = AccountUser.objects.get(uid=jsonpickle.loads(user).uid)
        if request.method == "POST":
            newemail = request.POST.get('newemail', None)
            acceptemail = request.POST.get('acceptemail', None)
            if changemail:
                if user.email != newemail:
                    if AccountUser.objects.filter(email=newemail).exists():
                        return HttpResponseRedirect('/user/index/?ecode=-4')
                    else:
                        user.email = newemail
                        user.verifyemail = 0
                        user.acceptemail = 0
                        user.save(update_fields=[
                                  'acceptemail', 'email', 'verifyemail'])
                        request.session['login'] = jsonpickle.dumps(user)
                        return HttpResponseRedirect('/user/index/?ecode=2')
                else:
                    return HttpResponseRedirect('/user/index/?ecode=-3')
            elif veremail and user.verifyemail == 0:
                send_email(user.email, user.salt)
                return HttpResponseRedirect('/user/index/?ecode=1')
            else:
                if acceptemail:
                    user.acceptemail = int(acceptemail)
                    user.save(update_fields=['acceptemail'])
                    request.session['login'] = jsonpickle.dumps(user)
                    return HttpResponseRedirect('/user/index/?ecode=2')
                else:
                    return HttpResponseRedirect('/user/index/')
        return render(request, 'user/index.html', locals())
    else:
        return HttpResponseRedirect('/login/?ecode=-1&url=%s' % request.get_full_path())


def verify(request):
    user = request.session.get('login', None)
    code = request.GET.get('code', None)
    errormsg = None
    if user:
        user = AccountUser.objects.get(uid=jsonpickle.loads(user).uid)

        if code and user.salt == code:
            user.verifyemail = 1
            user.acceptemail = 3
            user.save(update_fields=['verifyemail', 'acceptemail'])
            request.session['login'] = jsonpickle.dumps(user)
            return HttpResponseRedirect('/user/index/?ecode=3')
        else:
            return HttpResponseRedirect('/user/index/?ecode=-2')
    else:
        return HttpResponseRedirect('/login/?ecode=-1&url=%s' % request.get_full_path())
